<template>
	<view class="u-demo">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="behavior" label="违法行为" required>
				<uni-easyinput placeholder="违法行为" maxlength="-1" v-model="formData.behavior"
					trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="violation" maxlength="-1" label="违反条款" required>
				<uni-easyinput placeholder="违反条款" v-model="formData.violation" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="penalty" maxlength="-1" label="处罚条款" required>
				<uni-easyinput placeholder="处罚条款" v-model="formData.penalty" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="content" label="处罚内容" required>
				<uni-easyinput type="textarea" maxlength="-1" placeholder="处罚内容" v-model="formData.content"
					trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="doc" label="执法文书">
				<uni-file-picker ref="mfiles" return-type="array" @success="success" :auto-upload="false"
			    file-mediatype="all" file-extname="docx,doc,docm,dotx,dotm,xlsx,xls,xlsm,xlsb,xlt,xltx,xltm,pdf"
					v-model="formData.doc"></uni-file-picker>
			</uni-forms-item>
			<uni-forms-item name="category" label="行为类别" required>
				<uni-data-checkbox v-model="formData.category"
					:localdata="formOptions.category_localdata"></uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/xasa-behavior.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'xasa-behavior';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"behavior": "",
				"violation": "",
				"penalty": "",
				"content": "",
				"doc": [],
				"category": 0
			}
			return {
				formData,
				formOptions: {
					"category_localdata": [{
							"text": "经营主体",
							"value": 110,
							"icon": "icon"
						},
						{
							"text": "食品安全",
							"value": 120,
							"icon": "icon"
						},
						{
							"text": "特殊食品",
							"value": 125,
							"icon": "icon"
						},
						{
							"text": "反垄断、竞争",
							"value": 130,
							"icon": "icon"
						},
						{
							"text": "价格监管",
							"value": 140,
							"icon": "icon"
						},
						{
							"text": "药品监管",
							"value": 150,
							"icon": "icon"
						},
						{
							"text": "医疗器械",
							"value": 160,
							"icon": "icon"
						},
						{
							"text": "化妆品监管",
							"value": 170,
							"icon": "icon"
						},
						{
							"text": "市场合同",
							"value": 180,
							"icon": "icon"
						},
						{
							"text": "广告监管",
							"value": 190,
							"icon": "icon"
						},
						{
							"text": "知识产权",
							"value": 200,
							"icon": "icon"
						},
						{
							"text": "产品质量",
							"value": 210,
							"icon": "icon"
						},
						{
							"text": "标准管理",
							"value": 220,
							"icon": "icon"
						},
						{
							"text": "认证认可",
							"value": 230,
							"icon": "icon"
						},
						{
							"text": "计量监管",
							"value": 240,
							"icon": "icon"
						},
						{
							"text": "其他",
							"value": 999,
							"icon": "icon"
						}
					]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			success(e) {
			},
			async upload() {
				await this.$refs.mfiles.upload();
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				});
				this.$refs.form.validate().then((res) => {
					this.upload().then(() => {
						//res.doc = this.formData.doc.map(item => {
						//	return {
						//		extname: item.extname,
						//		name: item.name,
						//		url: item.url,
						//	    //filePath: item.filePath,
						//		size: item.size,
						//		fileID: item.fileID,
						//		uuid: item.uuid,
						//		cloudPath: item.cloudPath
						//	}	
						//});
						
						res.doc = this.formData.doc;
						// 提交数据
						return this.submitForm(res)
					})
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>